<template>
  <div class="app-container">
    <h1 style="text-align: center;margin-bottom: 2%;">{{parentTitle}}</h1>

    <!--sectionA-->
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <span style="margin-right: 1%">Section A</span>
      <el-button type="success" size="mini" icon="el-icon-circle-plus-outline" @click="add(1)">添加</el-button>
    </div>
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 15%;margin-right: 15%">
      <!-- 表格 -->
      <el-table :data="list.sectionA" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="title" label="标题" width="300" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>

        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openDialog(scope.row.id)">编辑
            </el-button>

            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--sectionB-->
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <span style="margin-right: 1%">Section B</span>
      <el-button type="success" size="mini" icon="el-icon-circle-plus-outline" @click="add(2)">添加</el-button>
    </div>
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 15%;margin-right: 15%">
      <!-- 表格 -->
      <el-table :data="list.sectionB" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="title" label="标题" width="300" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>

        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openDialog(scope.row.id)">编辑
            </el-button>

            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--sectionC-->
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%">
      <span style="margin-right: 1%">Section C</span>
      <el-button type="success" size="mini" icon="el-icon-circle-plus-outline" @click="add(3)">添加</el-button>
    </div>
    <div style="text-align: center;margin-bottom: 1%;margin-top: 1%;margin-left: 15%;margin-right: 15%">
      <!-- 表格 -->
      <el-table :data="list.sectionC" border stripe style="padding: 30px 0; text-align: center">
        <el-table-column prop="title" label="标题" width="300" align="center"/>
        <el-table-column prop="createTime" label="创建时间" width="200" align="center"/>

        <el-table-column label="操作" width="300" align="center">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="openDialog(scope.row.id)">编辑
            </el-button>

            <el-button type="danger" size="mini" icon="el-icon-error" @click="remove(scope.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 编辑弹窗-->
    <el-dialog
      title="阅读理解"
      :visible.sync="dialogVisible"
      width="50%"
      style="text-align: center">

      <el-form :inline="true">
        <el-form-item>
          标题<el-input v-model="listen.title" placeholder="输入标题" style="margin-bottom: 1%"/>
          问题<el-input type="textarea" :rows="4"  v-model="listen.question" placeholder="输入问题" style="margin-bottom: 1%"/>
          答案<el-input v-model="listen.answer" placeholder="输入答案" style="margin-bottom: 1%"/>
          序号<el-input v-model="listen.sort" placeholder="输入序号" style="margin-bottom: 1%"/>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="danger" @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import listenApi from '@/api/listen'

  export default {
    // 页内数据
    data() {
      return {
        list: [],
        page: 1,
        limit: 5,
        total: 0,
        subject: {},
        dialogVisible: false,
        title: '',
        listen: {},
        parentId: '',
        parentTitle: ''
      }
    },

    //初始化
    created() {
      this.parentId = this.$route.params.id
      this.parentTitle = this.$route.params.title
      this.fetchData()
    },

    methods: {
      //获取数据
      fetchData() {
        listenApi.getListen(this.parentId)
          .then(response => {
            this.list = response.data.list
          })
          .catch(error => {
            console.log(error)
          })
      },

      // 改变每页记录数
      changePageSize(size) {
        this.limit = size
        this.fetchData()
      },

      // 重置表单
      resetData() {
        this.title = ''
        this.fetchData()
      },

      // 添加分类
      add(section) {
        this.listen = {}
        this.listen.section = section
        this.listen.parentId = this.parentId
        this.dialogVisible = true
      },

      // 取消编辑
      cancel() {
        this.dialogVisible = false
        this.listen = {}
      },

      // 删除记录
      remove(id) {
        // 询问是否删除
        this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return listenApi.delete(id)
        }).then(response => {
          // 刷新页面
          this.fetchData()
          // 弹出成功提示
          this.$message({
            message: response.message,
            type: 'success'
          })
        })
      },

      //打开对话框
      openDialog(id) {
        this.dialogVisible = true
        listenApi.getListening(id)
          .then(response => {
            this.listen = response.data.item
          })
          .catch(error => {
            console.log(error)
          })
      },

      //更新分类
      update() {
        if (this.listen.id == null) {
          listenApi.add(this.listen)
            .then(response => {
              this.dialogVisible = false
              this.fetchData()
              this.listen = {}
            })
            .catch(error => {
              console.log(error)
            })
        } else {
          listenApi.update(this.listen)
            .then(response => {
              this.dialogVisible = false
              this.fetchData()
              this.listen = {}
            })
            .catch(error => {
              console.log(error)
            })
        }
      }
    }
  }
</script>
